package com.weng.cloud.service8881.async.controller;

import com.weng.cloud.service8881.async.service.TestAsyncService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @Author: weng
 * @Date: 2022/4/26
 * @Description: com.weng.cloud.service8881.async.controller
 */
@RestController
public class TestAsyncController {

    @Resource
    private TestAsyncService testAsyncService;

    @GetMapping("/async/testAsync")
    public void testAsync() throws ExecutionException, InterruptedException {
        CompletableFuture<String>[] cfs = new CompletableFuture[]{
                this.testAsyncService.busyMethod("jolin"),
                this.testAsyncService.busyMethod("david"),
                this.testAsyncService.busyMethod("babyface")
        };
        CompletableFuture.allOf(cfs).join();
        for (CompletableFuture<String> cf : cfs) {
            //要不玩玩stream api?
            System.err.println(cf.get());
        }
    }
}
